﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Infrastructure;
using Enterprise.Model.CaiWu;
using Enterprise.Data.CaiWu;
namespace Enterprise.Service.Caiwubu
{
    public class CwTypeService
    {
        /// <summary>
        ///  缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-CwType";

        /// <summary>
        /// 列表 返回所有记录
        /// </summary>
        /// <returns></returns>
        public List<CwTypeEntity> CwTypeList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<CwTypeEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                CwTypeData data = new CwTypeData();
                List<CwTypeEntity> list = data.CwTypeList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 组织机构记录 返回组织机构记录信息
        /// </summary>
        /// <param name="deptId">组织机构编码</param>
        /// <returns></returns>
        public CwTypeEntity CwTypeDisp(int typeId)
        {
            return CwTypeList().Where(p => p.TYPEID == typeId).FirstOrDefault();
        }

        

        private void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }


        /// <summary>
        /// 添加、修改、删除
        /// </summary>
        /// <param name="entity">组织机构实体</param>
        public void CwTypeExecute(CwTypeEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into CWB_TYPE(TYPEID,TNAME,TNAMERU,TQZ) values(SEQ_CWB_TYPE.nextval,'" + entity.TNAME + "'," + entity.TNAMERU + "," + entity.TQZ + ")";
                    break;
                case "Update":
                    cmd = "Update CWB_TYPE set TNAME='" + entity.TNAME + "' , TNAMERU=" + entity.TNAMERU + " , TQZ=" + entity.TQZ + " where TYPEID=" + entity.TYPEID;
                    break;
                case "Delete":
                    cmd = "Delete from CWB_TYPE where TYPEID=" + entity.TYPEID;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                CwTypeExecuteBySql(cmd, 1);
            }
        }

        /// <summary>
        /// 组织机构操作
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="typeId">1更新 0不更新</param>
        public void CwTypeExecuteBySql(string cmd, int typeId)
        {
            CwTypeData data = new CwTypeData();
            data.CwTypeExecute(cmd);
            if (typeId == 1)
            {
                ClearCache();
            }
        }

        #region "返回部门名称"
        /// <summary>
        /// 返回名称
        /// </summary>
        /// <param name="deptId">编号</param>
        /// <returns></returns>
        public static string GetTypeName(int typeId)
        {
            CwTypeService dService = new CwTypeService();
            var q = dService.CwTypeDisp(typeId);
            if (q != null)
            {
                return q.TNAME+"/"+q.TNAMERU;
            }
            return "";
        }
        #endregion
    }
}
